package org.abettor.pushbox.activity;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.download.DownloadUserInfo;
import org.abettor.pushbox.model.NotifyConfigBean;
import org.abettor.pushbox.model.SelfMessage;
import org.abettor.pushbox.model.UserInfoBean;
import org.abettor.pushbox.notify.NotifyUtil;
import org.abettor.pushbox.upload.UploadMap;
import org.abettor.pushbox.upload.UploadMap.UpdateNick;
import org.abettor.pushbox.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

public class OptionActivity extends Activity {
	private Button addNickButton, updatePassButton,notifyButton;
	private UploadMap uploadMap;
	private UpdateNick updateNick;
	// private TextView showNick;
	private Handler handler = null;

	// 按钮监听的监听器
	protected class ButtonOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			if (v == addNickButton) {
				uploadMap.addNickName(updateNick);
			} else if (v == updatePassButton) {
				uploadMap.updatePass();// 更新密码的操作
			}else if(v == notifyButton){
				NotifyConfigBean notify = Config.readNotifyConfigBean(OptionActivity.this);
				if(!notify.getIsOpenNotify().equals(NotifyConfigBean.OPENNOTIFY)){
					notify.setIsOpenNotify(NotifyConfigBean.OPENNOTIFY);
					//进行启动服务的操作
					 NotifyUtil.startNotifyService(OptionActivity.this);
				}else{
					notify.setIsOpenNotify(NotifyConfigBean.UNOPENNOTIFY);
				}
				Config.writeNotifyConfigBean(OptionActivity.this, notify);
				setButtonState();
			}
		}
	}
	private void setButtonState(){
		NotifyConfigBean notify = Config.readNotifyConfigBean(this);
		if(!notify.getIsOpenNotify().equals(NotifyConfigBean.OPENNOTIFY)){
			notifyButton.setText(this.getText(R.string.option_activity_open_notify));
		}else{
			notifyButton.setText(this.getText(R.string.option_activity_close_notify));
		}
	}

	// 显示表格的操作
	private void showTable(String nickName) {
		DownloadUserInfo.UpadateView view = new DownloadUserInfo.UpadateView() {
			@Override
			public void update(UserInfoBean userInfo) {
				showTableOperation(userInfo);
			}
		};
		DownloadUserInfo downInfo = new DownloadUserInfo(this, view);
		downInfo.readUserInfo(nickName);
	}

	// 显示表格的操作
	private void showTableOperation(UserInfoBean userInfo) {
		LinearLayout mynick = (LinearLayout) this.findViewById(R.id.mynick);
		mynick.setVisibility(View.GONE);
		TableLayout table = (TableLayout) this.findViewById(R.id.myinfo);
		table.setVisibility(View.VISIBLE);
		// 往表格里填数据的操作
		TextView showNick = (TextView) this.findViewById(R.id.showNick);
		TextView showRegtime = (TextView) this.findViewById(R.id.showRegtime);
		TextView showHaspass = (TextView) this.findViewById(R.id.showHaspass);
		TextView showMyCreate = (TextView) this.findViewById(R.id.showMyCreate);
		TextView showMyUpload = (TextView) this.findViewById(R.id.showMyUpload);
		TextView showMyResolved = (TextView) this
				.findViewById(R.id.showMyResolved);
		TextView showMyQuesstion = (TextView) this
				.findViewById(R.id.showMyQuesstion);
		TextView showMyAnswer = (TextView) this.findViewById(R.id.showMyAnswer);
		showNick.setText(userInfo.getNickName());
		showRegtime.setText(userInfo.getRegtime());
		showHaspass.setText(userInfo.getMyPassCount());
		showMyCreate.setText(userInfo.getMyCreateCount());
		showMyUpload.setText(userInfo.getMyUploadCount());
		showMyResolved.setText(userInfo.getMyResolvedCount());
		showMyQuesstion.setText(userInfo.getMyQuesstionCount());
		showMyAnswer.setText(userInfo.getMyAnswerCount());
	}

	// 隐藏表格的操作
	private void hiddenTable() {
		TableLayout table = (TableLayout) this.findViewById(R.id.myinfo);
		table.setVisibility(View.GONE);
		LinearLayout mynick = (LinearLayout) this.findViewById(R.id.mynick);
		mynick.setVisibility(View.VISIBLE);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.option_activity);
		uploadMap = new UploadMap(this);
		addNickButton = (Button) this.findViewById(R.id.addNick);
		addNickButton.setOnClickListener(new ButtonOnClickListener());
		updatePassButton = (Button) this.findViewById(R.id.updatePass);
		updatePassButton.setOnClickListener(new ButtonOnClickListener());
		notifyButton = (Button) this.findViewById(R.id.notify);
		notifyButton.setOnClickListener(new ButtonOnClickListener());
		setButtonState();
		SelfMessage self = Config.readSelfMessage(this);
		if (self == null) {
			hiddenTable();
		} else {
			showTable(self.getNickName());
		}
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				SelfMessage self = Config.readSelfMessage(OptionActivity.this);
				showTable(self.getNickName());
			}
		};
		// 更新昵称成功的操作,设置按钮的动作
		updateNick = new UpdateNick() {
			@Override
			public void update() {
				OptionActivity.this.handler.sendEmptyMessage(0);
			}
		};
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
//		Intent intent = this.getIntent();
//		Bundle extras = intent.getExtras();
//		if (extras == null) {
//			super.onBackPressed();
//			return;
//		}
//		String className = extras.getString("class_name");
//		if ("NetUserMapActivity".equals(className)) {
//			Intent in = new Intent();
//			in.setClass(OptionActivity.this, NetUserMapActivity.class);
//			this.startActivity(in);
//			this.finish();
//		} else {
//			super.onBackPressed();
//		}
	}
}
